﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using Login;
using UI;
#if !NAKED
using supersdkUnity;
#endif
public class LoginUI : MonoBehaviour
{
    public LoginManager manager;

    public GameObject loginBG;
    public LoginUpdateUI updatePanel;              // 更新部分，进度条，说明文字
    public LoginNormalUI loginNormalPanel;               // 登陆部分，登陆按钮，切换账号，版本号，CopyRight，当前服务器
    public ServerPanel serverPanel;             // 服务器信息
    public GameObject choseRolePanel;           // 初始化选人

    public void Init()
    {
        loginBG.SetActive(true);
        updatePanel.Hide();
        loginNormalPanel.Hide();
        choseRolePanel.SetActive(false);


        GameObject go = Instantiate(loadingParticle) as GameObject;
        go.transform.SetParent(loadingParent.transform);
        go.transform.localPosition = new Vector3(0, 0, 0);
        go.transform.localScale = Vector3.one;
        go.transform.localEulerAngles = Vector3.zero;
        go.SetActive(true);

        clickObject = Instantiate(clickParticle) as GameObject;
        clickObject.transform.SetParent(particleParent.transform);

        clickObject.transform.localPosition = new Vector3(0, 0, -300);
        clickObject.transform.localScale = Vector3.one;
        clickObject.transform.localEulerAngles = Vector3.zero;
        clickObject.SetActive(false);

    }

    public void ShowNormalUI()
    {
        loginNormalPanel.Show();

    }

    public void HideNormalUI()
    {
        loginNormalPanel.Hide();
    }

    public void ShowUpdateUI()
    {
        updatePanel.GetComponent<LoginUpdateUI>().Show();
    }

    public void HideUpdateUI()
    {
        updatePanel.GetComponent<LoginUpdateUI>().Hide();
    }

    public void ShowServerPanel() {
        serverPanel.Show();
    }

    public void HideServerPanel()
    {
        serverPanel.Hide();
    }

    public UILabel gameVersion;
    public GameObject loginBtn;
    public GameObject yingyongbaoLoginBtn;
    public GameObject clickParticle;
    public GameObject particleParent;
    public GameObject mask;
    public GameObject loadingParticle;
    public GameObject loadingParent;

    private GameObject clickObject;
    public static bool firstCoroutine = true;

    /// <summary>
    /// 显示应用宝登录按钮
    /// </summary>
    public void ShowYYBLoginBtn() 
    {
        loginBtn.SetActive(false);
        yingyongbaoLoginBtn.SetActive(true);
    }
    public void QQLogin() 
    {
		#if !NAKED
        PlatformSDK.GetInstance().OtherLogin(manager);
		#endif
    }

    public void WeiXinLogin() 
    {
		#if !NAKED
        PlatformSDK.GetInstance().Login(manager);
		#endif
    }
    /// <summary>
    /// 登录
    /// </summary>
    public void OnPressLogin()
    {
      
        if (!manager.isAuthPassed)
        {
            if (GameConfig.Instance.platform == 1)
            {
                loginNormalPanel.ShowInternalAccountPanel();
            }
            else if (GameConfig.Instance.platform == 2)
            {
                if (GameConfig.Instance.GetChannelId == ChannelId.YingYongBao) 
                {
                    return;
                }
                else
                {
					#if !NAKED
                    PlatformSDK.GetInstance().Login(manager);
					#endif
                }
               
            }
        }
        else
        {
            if (!string.IsNullOrEmpty(manager.ZoneIp))
            {
                if (StaticData.isLogining)
                {
                    return;
                }
                else
                {
                    StaticData.isLogining = true;
                    StopCoroutine("StartLogin");
                    StartCoroutine(StartLogin(1.3f));
                }
            }
            else
            {
                BoxManager.CreatePopupTextBox(TableTextBlo.GetText("net_error"));
                return;
            }
        }


    }

    IEnumerator StartLogin(float time)
    {
        clickObject.SetActive(true);
        loginNormalPanel.loginTextSprite.spriteName = "UI_denglu_word_zhengzai";
        loginNormalPanel.loginTextSprite.MakePixelPerfect();

        yield return new WaitForSeconds(time);
        clickObject.SetActive(false);
        firstCoroutine = false;
      
        StartServer();
    }

    void StartServer()
    {
        StaticData.httpServer = manager.GetCurrentServerUrl();
        if (!string.IsNullOrEmpty(StaticData.httpServer))
        {
            NetAPI.ResetHttpServerUrl(StaticData.httpServer);
            Debug.Log(manager.GetCurrentServerUrl());
            Debug.Log("当前链接的服务器ID是：" + StaticData.playerData.ZoneId + ";服务器名字是：" + manager.ZoneName + ";ip = " + manager.ZoneIp);
            //yield return new WaitForSeconds(0.7f);
            mask.SetActive(false);
            manager.SwitchState(LoginState.UserLogin);
        }
        else
        {
            BoxManager.CreatePopupTextBox(TableTextBlo.GetText("net_error"));

        }
    }

    public void OnSwitchAccountButtonClick()
    {
        manager.CleanCache();//清空缓存
        if (GameConfig.Instance.platform == 1)
        {
            serverPanel.Hide();
            manager.SwitchState(LoginState.LoginAuth);
        }
        else
        {
#if !NAKED
            if (SuperSDKUnity3D.isSupported(SuperSDKDataKeys.Function.SWITCH_ACCOUNT))
            {

                PlatformSDK.GetInstance().SwitchAccount();

            }
#endif
        }
    }






    public void NextScene()
    {
        manager.LoginDone();
    }


}
